#include "Rules.h"
#include "Card.h"
#include "Players.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

/* LOGIC:
 * The Heart of the Program
 */



class Logic {
        
        Rules *rules;
        Card allCards[52];
        
        void shuffle(){
                int n = 52;
                Card *c = new Card;
                while ( n > 1 ) {
                        int k = (int) (lrand48() % n);
                        --n;
                        *c = allCards[n];
                        allCards[n] = allCards[k];
                        allCards[k] = *c;
                }
                delete c;
        }
        list<Card> getLegalMoves(int pNum);
        int getStartPlayer();
        
        public:
        Player *players[4];

        Logic(){
                
                //Allows for character input
                char in;
                cout << "Would you like to play the regular(r) or alternate(a) version of the game?" << endl;
                cin >> in;
                
                //Play by the Normal Rules
                if(in ==  'r'){
                        rules = new normRules;
                }
                //Play by the Alternate Rules
                if(in == 'a'){
                        rules = new altRules;
                }
                //Initialize the Players
                for(int i = 1; i <= 4; i++){
                        cout << "Is player " << i << " a human(h) or a computer(c)?" << endl;
                        cout << ">";
                        cin >> in;
                        
                        if(in == 'h'){
                                players[i-1] = new hPlayer;
                                players[i-1]->setPNum(i);
                        }
                        else if(in == 'c'){
                                players[i-1] = new cPlayer;
                                players[i-1]->setPNum(i);
                        }
                        
                }
                //Initialize the Cards
                int num = 0;
                for(int i = 1; i <= 4; i++){
                        for(int j = 1; j <= 13; j++){
                                switch(i){
                                        case 1: allCards[num].suit_ = CLUB; break;
                                        case 2: allCards[num].suit_ = DIAMOND; break;
                                        case 3: allCards[num].suit_ = HEART; break;
                                        case 4: allCards[num].suit_ = SPADE; break;
                                }
                                switch(j){
                                        case 1: allCards[num].rank_ = ACE; break;
                                        case 2: allCards[num].rank_ = TWO; break;
                                        case 3: allCards[num].rank_ = THREE; break;
                                        case 4: allCards[num].rank_ = FOUR; break;
                                        case 5: allCards[num].rank_ = FIVE; break;
                                        case 6: allCards[num].rank_ = SIX; break;
                                        case 7: allCards[num].rank_ = SEVEN; break;
                                        case 8: allCards[num].rank_ = EIGHT; break;
                                        case 9: allCards[num].rank_ = NINE; break;
                                        case 10: allCards[num].rank_ = TEN; break;
                                        case 11: allCards[num].rank_ = JACK; break;
                                        case 12: allCards[num].rank_ = QUEEN; break;
                                        case 13: allCards[num].rank_ = KING; break;
                                }
                                num++;
                        }
                }
                this->shuffle();
                //Add the actually giving out of the cards
                num = 0;
                for(int pNum = 0; pNum < 4; pNum++){
                        for(int card = 0; card < 13; card++){
                                players[pNum]->cArray.push_back(allCards[num]);
                                num++;
                        }
                }

        }
        void Reset(){
                this->shuffle();
                //Add the actually giving out of the cards
                int num = 0;
                for(int pNum = 0; pNum < 4; pNum++){
                        for(int card = 0; card < 13; card++){
                                players[pNum]->cArray.push_back(allCards[num]);
                                num++;
                        }
                }
        }
        list<Card> listInitCard(int);
        void startGame();
        void executeCommand(Command command, int pNum);
        void printDeck();
        void assignScores();
        void emptyDiscards();
};

